{template 'common/header'}
<ul class="nav nav-tabs">
	<li {if $op == 'display'}class="active"{/if}><a href="{php echo $this->createWebUrl('mytoken', array('op'=>'display','merchantid'=>$merchantid));}">管理优惠券</a></li>
	<li {if $op == 'post' && !$couponid}class="active"{/if}><a href="{php echo $this->createWebUrl('mytoken', array('op'=>'post','merchantid'=>$merchantid));}">添加优惠券</a></li>
	{if $op == 'post' &&  $couponid}<li class="active"><a href="{php echo $this->createWebUrl('mytoken', array('id'=>$couponid,'merchantid'=>$merchantid));}">编辑优惠券</a></li>{/if}
	<li {if $op == 'record'}class="active"{/if}><a href="{php echo $this->createWebUrl('mytoken', array('op'=>'record','id'=>$couponid,'merchantid'=>$merchantid));}">优惠券兑换记录</a></li>
</ul>
{if $op == 'post'}
<style>
	.text-danger{color:red}
</style>
<div class="main">
	<form action="" method="post" class="form-horizontal form" enctype="multipart/form-data" id="form1">
		<input type="hidden" name="merchantid" id="merchantid" value="{$merchantid}" />
		<div class="panel panel-default" id="step1">
			<div class="panel-heading">
				优惠券
			</div>
			<div class="panel-body">
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 优惠券名称</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="title" class="form-control" value="{$item['title']}" />
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 减免金额</label>
					<div class="col-sm-9 col-xs-12">
						<div class="input-group">
							<input type="text" name="discount" class="form-control"  value="{$item['discount']}" />
							<span class="input-group-addon">元</span>
						</div>
						<span class="help-block">可减免金额。</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 抵扣条件</label>
					<div class="col-sm-9 col-xs-12">
						<div class="input-group">
							<input type="text" name="condition" class="form-control" value="{$item['condition']}" />
							<span class="input-group-addon">元</span>
						</div>
						<span class="help-block">消费满多少元可用。如不填写则默认：消费满任意金额可用</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 期限</label>
					<div class="col-sm-9 col-xs-12">
						<input type="text" name="daylimit" class="form-control" value="{$item['daylimit']}" />
						<span class="help-block">客户领取优惠券后后多少天有效。</span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 缩略图</label>
					<div class="col-sm-9 col-xs-12">
						{php echo tpl_form_field_image('thumb', $item['thumb'])}
					</div>
				</div>
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label"><span class="text-danger">*</span> 优惠券说明</label>
					<div class="col-sm-9 col-xs-12">
						{php echo tpl_ueditor('description', $item['description'])}
					</div>
				</div>
			</div>
			
		</div>
		<div class="form-group">
			<div class="col-sm-12">
				<input name="submit" id="submit" type="submit" value="提交" class="btn btn-primary col-lg-1">
				<input name="id" type="hidden" value="{$item['couponid']}">
				<input type="hidden" name="token" value="{$_W['token']}" />
			</div>
		</div>
	</form>
	<div id="footer-location"  class="hide">
		<span name="submit" id="submit" class="pull-right btn btn-primary">保存</span>
	</div>
</div>
<script type="text/javascript">
	$("#form1").submit(function(){
		if($.trim($(':text[name="title"]').val()) == "") {
			util.message("请填写优惠券名称",'','error');
			return false;
		}
		var discount = parseInt($.trim($(':text[name="discount"]').val()));
		if(isNaN(discount)) {
			util.message("请填写可抵消的金额",'','error');
			return false;
		}
		if($.trim($('input[name="thumb"]').val()) == "") {
			util.message("上传优惠券缩略图",'','error');
			return false;
		}
		if($.trim($('textarea[name="description"]').val()) == "") {
			util.message("请填写优惠券说明",'','error');
			return false;
		}
		return true;
	});
</script>
{elseif $op == 'display'}
<div class="main">
	<div class="panel panel-info">
	<div class="panel-heading"><a href="{php echo $this->createWebUrl('merchant')}">返回商家</a></div>
	</div>
</div>
<!--<div class="alert alert-info">
	如果您希望在会员在线上消费时，可以使用优惠券来减免金额，请确保 <a href="{php echo url('profile/payment')}" target="_blank">支付参数</a> 中的卡券开关为："使用系统卡券"
</div>-->
<div class="panel panel-default">
	<div class="panel-body table-responsive">
		<table class="table table-hover table-bordered">
			<thead class="navbar-inner">
			<tr>
				<th style="width:60px;" class="text-center">缩略图</th>
				<th style="width:100px;" class="text-center">标题</th>
				<th style="width:120px;" class="text-center">序列号</th>
				<th style="width:100px;" class="text-center">使用条件</th>
				<th style="width:80px;" class="text-center">面值</th>
				<th style="width:80px;" class="text-center">期限</th>
				<th style="width:150px;" class="text-center">操作</th>
			</tr>
			</thead>
			<tbody>
			{loop $list $item}
			<tr>
				<td class="text-center"><img src="{$item['thumb']}" width="40"></td>
				<td class="text-center">{$item['title']}</td>
				<td class="text-center">{$item['couponsn']}</td>
				<td class="text-center">{if $item['condition']}满{$item['condition']}元可用{else}任意金额可用{/if}</td>
				<td class="text-center">{$item['discount']} 元</td>
				<td class="text-center">{$item['daylimit']} 天</td>
				<td class="text-center">
					<a href="{php echo $this->createWebUrl('mytoken', array('id' => $item['couponid'],'op'=>'post','merchantid'=>$merchantid));}" title="编辑">编辑</a>&nbsp;-&nbsp;
					<a href="{php echo $this->createWebUrl('mytoken', array('id' => $item['couponid'],'op'=>'del','merchantid'=>$merchantid));}" onclick="return confirm('此操作不可恢复，确认删除？');return false;" title="删除">删除</a>&nbsp;-&nbsp;
					<a href="{php echo $this->createWebUrl('mytoken', array('couponid' => $item['couponid'],'op'=>'record','merchantid'=>$merchantid));}" title="兑换记录">兑换记录</a>
				</td>
			</tr>
			{/loop}
			</tbody>
		</table>
	</div>
</div>
{$pager}
</div>
{elseif $op == 'record'}
<div class="main">
	<div class="panel panel-info">
		<div class="panel-heading">筛选</div>
		<div class="panel-body">
			<form action="./index.php" method="get" class="form-horizontal" role="form">
			<input type="hidden" name="c" value="site">
			<input type="hidden" name="a" value="entry">
			<input type="hidden" name="m" value="weliam_indiana" />
			<input type="hidden" name="do" value="mytoken">
			<input type="hidden" name="op" value="record">
			<input type="hidden" name="id" value="{$id}">
				<input type="hidden" name="merchantid" value="{$merchantid}">
				<div class="form-group">
					<label class="col-xs-12 col-sm-3 col-md-2 control-label">兑换标题</label>
					<div class="col-sm-6 col-lg-8 col-xs-12">
						<select class="form-control" name="couponid">
							<option value="0">不限</option>
							{loop $coupons $coupon}
								<option value="{$coupon['couponid']}" {if $_GPC['couponid'] == $coupon['couponid']}selected{/if}>{$coupon['title']}</option>
							{/loop}
						</select>	
						<button class="btn btn-default"><i class="fa fa-search"></i> 搜索</button>
					</div>
				</div>
			</form>
		</div>
	</div>
	<div class="panel panel-default">
	<div class="table-responsive panel-body">
		<table class="table table-hover table-bordered">
			<thead class="navbar-inner">
				<tr>
					<th style="width:80px; text-align:center;">用户ID</th>
					<th style="width:80px; text-align:center;">标题</th>
					<th style="width:80px; text-align:center;">图标</th>
					<th style="width:100px; text-align:center;">面额</th>
					<th style="width:150px; text-align:center;">兑换时间</th>
					<th style="width:150px; text-align:center;">发放模块</th>
					<th style="width:150px; text-align:center;">优惠券状态</th>
					<th style="width:100px;">操作者</th>
					<th style="width:150px; text-align:center;">使用时间</th>
					<th style="width:140px; text-align:center;">操作</th>
				</tr>
			</thead>
			<tbody>
				{loop $list $item}
				<tr>
					<td class="text-center">{$item['uid']}</td>
					<td class="text-center">{$item['title']}</td>
					<td class="text-center"><img src="{$item['thumb']}" style="max-width:50px; max-height: 30px;"></td>
					<td class="text-center">{$item['discount']}</td>
					<td class="text-center">{php echo date('Y-m-d H:i', $item['granttime'])}</td>
					<td class="text-center">
						{if !empty($item['grantmodule'])}
							<span class="label label-warning">{$modules[$item['grantmodule']]['title']}</span>
						{/if}
					</td>
					<td class="text-center">
						{if $item['status'] == 1}
							<span class="label label-success">未使用</span>
						{else}
							<span class="label label-danger">已使用</span>
						{/if}
					</td>
					<td>
						{if $item['status'] == 1}
							<button type="button" class="btn btn-danger btn-sm" data-toggle="modal" data-target="#tabused" data-cid="{$item['couponid']}" data-rid="{$item['recid']}"
							data-uid="{$item['uid']}" onclick="$('#couponid').val($(this).attr('data-cid'));$('#uid').val($(this).attr('data-uid'));$('#recid').val($(this).attr('data-rid'))">标记为使用</button>
						{else}
							{$item['operator']}
						{/if}
					</td>
					<td class="text-center">
						{if $item['status'] == 1}
						{else}
							{php echo date('Y-m-d H:i', $item['usetime'])}
						{/if}
					</td>
					<td class="text-center">
						<a onclick="if(!confirm('删除后不可恢复,您确定删除吗?')) return false;"  href="{php echo $this->createWebUrl('mytoken', array('id' => $item['recid'],'op'=>'record-del','merchantid'=>$merchantid));}" title="删除兑换记录">删除兑换记录</a>
					</td>
				</tr>
				{/loop}
			</tbody>
		</table>
	</div>
	</div>
	{$pager}
</div>

<!-- 优惠券标记已使用模态层 -->
<!--<div class="modal fade" id="tabused" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
	<div class="modal-dialog">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
				<h4 class="modal-title" id="myModalLabel">将折扣券标记为使用</h4>
			</div>
			<form action="" method="post" class="form-horizontal" role="form">
			<div class="modal-body">
				请输入店员操作密码：
				<input type="text" name="password" class="form-control" />
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-default" data-dismiss="modal">关闭</button>
				<button type="submit" name="submit" class="btn btn-danger" value="used" />标记为已使用</button>
				<input type="hidden" name="uid" id="uid" value="" />
				<input type="hidden" name="couponid" id="couponid" value="" />
				<input type="hidden" name="recid" id="recid" value="" />
				<input type="hidden" name="mytoken" value="{$_W['mytoken']}" />
				<input type="hidden" name="mytoken" value="{$_W['mytoken']}" />
			</div>
			</form>
		</div>
	</div>
</div>-->

{/if}
{template 'common/footer'}